export default {
  state() {
    return {
      cityName: localStorage.getItem("cityname") || "全国",
      cityId: localStorage.getItem("cityId") || "11111",

      cityList: [],
    };
  },
  getters: {
    hotList(state) {
      return state.cityList[0];
    },
    otherList(state) {
      return state.cityList.slice(1);
    },
  },
  mutations: {
    changeCity(state, payload) {
      state.cityName = payload.name;
      state.cityId = payload.cityId;
      localStorage.setItem("cityname", payload.name);
      localStorage.setItem("cityId", payload.cityId);
    },
    getCityList(state, payload) {
      state.cityList = payload;
    },
  },
  actions: {
    getCityListAsync(context) {
      fetch("http://101.35.148.193:3000/api/city/list")
        .then((response) => response.json())
        .then((res) => {
          if (res.success) {
            context.commit("getCityList", res.data);
          }
        });
    },
  },
};
